package com.cxs.drawpointdemo.slice;

import com.cxs.drawpointdemo.ResourceTable;
import com.cxs.drawpointdemo.constant.Common;
import com.cxs.drawpointdemo.data.Question;
import com.cxs.drawpointdemo.util.KvStoreUtils;
import com.cxs.drawpointdemo.util.ZSONUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.service.WindowManager;
import ohos.data.distributed.common.ChangeNotification;
import ohos.data.distributed.common.Entry;
import ohos.data.distributed.common.KvStoreObserver;
import ohos.data.distributed.common.SubscribeType;
import ohos.data.distributed.user.SingleKvStore;

import java.util.List;

public class DrawAbilitySlice extends AbilitySlice {

    private static SingleKvStore kvStore = null;
    private DrawObServer observer;
    Button btnSend, btnReselect, btnSendTip1, btnSendTip2, btnRestart;
    TextField tfInput;
    Text textTitle, textContent;
    private static Question question;

    class DrawObServer implements KvStoreObserver {
        @Override
        public void onChange(ChangeNotification changeNotification) {
            System.out.println("DrawObServer.onChange");
            List<Entry> insertEntries = changeNotification.getInsertEntries();
            List<Entry> updateEntries = changeNotification.getUpdateEntries();
            insertEntries.forEach(entry -> changeNotifi(entry));
            updateEntries.forEach(entry -> changeNotifi(entry));
        }
    }

    private void changeNotifi(Entry entry) {
        String key = entry.getKey();
        switch (key) {
            case Common.INPUT_MESSAGE_KEY:
                // 接收消息
                sendMessage(entry.getValue().getString());
                break;
            case Common.RESULT_KEY:
                // 猜测结果
                int result = entry.getValue().getInt();
                if (result == Common.SUCCESSFUL) {
                    // 猜对了
                }
                break;
        }
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_draw);

        initDB();
        initComponent();
        initQuestion();
    }

    /**
     * 初始化题目
     */
    private void initQuestion() {
        question = ZSONUtils.getQuestion(getContext());
        textTitle.setText(question.getTitle());
        kvStore.putString(Common.QUESTION_TITLE_KEY, question.getTitle());
        kvStore.putString(Common.QUESTION_DESCRIBE_KEY, question.getDescribe());
    }

    private void initComponent() {

        // 题目文本框
        textTitle = (Text) findComponentById(ResourceTable.Id_text_draw_question_title);
        // 重新选词
        btnReselect = (Button) findComponentById(ResourceTable.Id_btn_reselect);
        btnReselect.setClickedListener(c -> {
            initQuestion();
            kvStore.putString(Common.RESELECT_QUESTION_KEY, question.getTitle());
        });
        // 提示1
        btnSendTip1 = (Button) findComponentById(ResourceTable.Id_btn_send_tip1);
        btnSendTip1.setClickedListener(c -> {
            sendMessage(question.getTip1());
            kvStore.putString(Common.QUESTION_FIRST_TIP_KEY, question.getTip1());
        });
        // 提示2
        btnSendTip2 = (Button) findComponentById(ResourceTable.Id_btn_send_tip2);
        btnSendTip2.setClickedListener(c -> {
            sendMessage(question.getTip2());
            kvStore.putString(Common.QUESTION_SECOND_TIP_KEY, question.getTip2());
        });
        btnRestart = (Button) findComponentById(ResourceTable.Id_btn_restart);
        btnRestart.setClickedListener(c -> {
            // 重新开始
            initQuestion();
            sendMessage("重新开始");
            kvStore.putString(Common.RESTART_KEY, question.getTitle());
        });
        // 消息输入框
        tfInput = (TextField) findComponentById(ResourceTable.Id_tf_input_message);
        getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);
        // 消息显示
        textContent = (Text) findComponentById(ResourceTable.Id_text_message_content);
        // 发送按钮
        btnSend = (Button)findComponentById(ResourceTable.Id_btn_send);
        btnSend.setClickedListener(c -> {
            kvStore.putString(Common.INPUT_MESSAGE_KEY, tfInput.getText());
            sendMessage(tfInput.getText());
            tfInput.setText("");
        });
    }

    private void initDB() {
        kvStore = KvStoreUtils.getKvStore();
        observer = new DrawObServer();
        kvStore.subscribe(SubscribeType.SUBSCRIBE_TYPE_REMOTE, observer);
    }

    private void sendMessage(String message) {
        getUITaskDispatcher().asyncDispatch(() -> {
            textContent.append(message + "\n");
        });
    }
}
